﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class MainCommitteeDepositeViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;
        /// <summary>
        /// Initializes a new instance of the <see cref="MainCommitteeDepositeViewModel"/> class.
        /// </summary>
        /// <param name="administrationRepository">The administration repository.</param>
        /// <param name="financeService">The finance service.</param>
        public MainCommitteeDepositeViewModel(IAdministrationRepository administrationRepository, IFinanceService financeService)
        {
            _AdministrationRepository = administrationRepository;
            _FinanceService = financeService;
        }

        /// <summary>
        /// Gets the main branch deposites.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns></returns>
        public IEnumerable GetMainBranchDeposites(Guid projectID)
        {
            Guid bankAccountID = GetMainCommitteeBankAccountID(projectID);
            return from deposit in _AdministrationRepository.FindMainCommitteeDeposites(commDepo => commDepo.CommitteBankAccountID.Equals(bankAccountID))
                   select new
                   {
                       DepositDate = deposit.DepositDate,
                       Amount = deposit.Amount,
                       ProjectName = deposit.CommitteeBankAccount.Project.ProjectName
                   };
        }

        /// <summary>
        /// Deposits the in main committee.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <param name="amount">The amount.</param>
        /// <param name="depositDate">The deposit date.</param>
        public void DepositInMainCommittee(Guid projectID, double amount, DateTime depositDate)
        {
            using (IUnitOfWork tranc = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                Guid bankAccountID = GetMainCommitteeBankAccountID(projectID);

                IMainCommitteeDeposit mainCommitteeDeposit = ServiceLocator.Current.GetInstance<IMainCommitteeDeposit>();
                mainCommitteeDeposit.MainCommitteeDepositsID = Guid.NewGuid();
                mainCommitteeDeposit.CommitteBankAccountID = bankAccountID;
                mainCommitteeDeposit.Amount = amount;
                mainCommitteeDeposit.DepositDate = depositDate;
                _AdministrationRepository.CreateMainCommitteeDeposite(mainCommitteeDeposit);

                tranc.Commit();
            }
        }
        /// <summary>
        /// Gets the main committee bank account ID.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns></returns>
        private Guid GetMainCommitteeBankAccountID(Guid projectID)
        {
            var mainBranch = _AdministrationRepository.FindCommittees(comm => comm.IsMainCommittee == true).First();
            return mainBranch.CommitteeBankAccounts.Where(bankAcc => bankAcc.ProjectID.Equals(projectID)).First().CommitteeBankAccountID;
        }

        /// <summary>
        /// Gets the main branch balance.
        /// </summary>
        /// <returns></returns>
        public double GetMainBranchBalance()
        {
            return _FinanceService.GetMainCommitteeBalance();
        }

        /// <summary>
        /// Gets the name of the main branch.
        /// </summary>
        /// <returns></returns>
        public string GetMainBranchName()
        {
            var committee =_AdministrationRepository.FindCommittees(comm => comm.IsMainCommittee == true).FirstOrDefault();
            if (committee != null)
                return committee.CommitteeName;
            return string.Empty;
        }

        /// <summary>
        /// Gets the user project ID.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetUserProjects()
        {
            return _AdministrationRepository.GetUserProjects(RegistryInfo.UserID);
        }
    }
}
